//
//  State_cGame.m
//  GiveWay
//
//  Created by Nhan Nguyen on 9/18/13.
//  Copyright 2013 nhan.nguyenngoc. All rights reserved.
//

#import "State_cGame.h"
#define IOS_3 320
#define IOS_4 640

@implementation State_cGame
+(CCScene *) scene
{
    
	CCScene *scene = [CCScene node];
	State_cGame *layer = [State_cGame node];
	[scene addChild: layer];
	return scene;
}


-(id) init
{
	if( (self=[super init])) {
        
        //glColorMagk(true, false, true, false);
        CGSize size = [[CCDirector sharedDirector] winSize];
        SCR_W = size.width;
        SCR_H = size.height;
         _touchEnabled = YES;

        [self draw];

    }
	
	return self;
}

-(void) draw
{
    
    [super draw];
    [self debug];
    
}


-(void) debug 
{
    
    
    int x,y,w,h,temp;
    switch (SCR_H) {
        case IOS_3:
            x=90;
            temp=y=10;
            w=h=50;
           break;
        case IOS_4:
            x=180;
            temp=y=20;
            w=h=100;
            break;
        default:
            break;
    }
    glLineWidth(3.0f);
    ccDrawColor4F(0.8, 1.0, 0.76, 1.0);
        for(int i=0;i<=6;i++)
    {
        ccDrawLine(ccp(x, y), ccp(SCR_W-x,y));
        y+=h;
    }
    for(int i=0;i<=6;i++)
    {
        ccDrawLine(ccp(x, 10), ccp(x,SCR_H-temp));
        x+=w;
    }
    ccDrawPoint(CGPointMake(100, 100));
    
    
}

-(int) Funtion_chek_touch :(CGPoint)location
{
    int x,y,w,h,temp;
    switch (SCR_H) {
        case IOS_3:
            x=90;
            temp=y=10;
            w=h=50;
            break;
        case IOS_4:
            x=180;
            temp=y=20;
            w=h=100;
            break;
        default:
            break;
    }

    int current_posX,current_posY,number_W,number_H;
    number_W = 6;
    number_H = 6;
    current_posX = (location.x - x)/w;
    current_posY = (location.y - y)/h;
    
    if(CGRectContainsPoint(CGRectMake(x, y,6*w, 6*h), location))
        return current_posX + number_W*current_posY;
    else
        return -1;
   // NSLog(@"current_posX + number_W*current_posY========%d",current_posX + number_W*current_posY);
}

-(void) registerWithTouchDispatcher
{
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
	location = [[CCDirector sharedDirector] convertToGL:location];
    int example = [self Funtion_chek_touch:location];
    
     NSLog(@"current_posX + number_W*current_posY========%d",example);
     NSLog(@"cGame_bg.position.x========%f",cGame_bg.position.x);
     NSLog(@"cGame_bg.position.y========%f",cGame_bg.position.y);
    
	return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
	location = [[CCDirector sharedDirector] convertToGL:location];
    
    
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
	location = [[CCDirector sharedDirector] convertToGL:location];
    cGame_bg.visible=false;
    
    //    [self move_right];
    
    
}


@end
